using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;

namespace Se.Framework.Web.Mvc.GenericCrud
{
    public class PropertiesOrderComparer : IComparer<DisplayAttribute>
    {
        public int Compare(DisplayAttribute x, DisplayAttribute y)
        {
            var orderx = x;
            var ordery = y;

            if ((orderx == null || !orderx.GetOrder().HasValue) && (ordery == null || !ordery.GetOrder().HasValue))
            {
                return 0;
            }

            if (orderx == null || !orderx.GetOrder().HasValue)
            {
                return 1;
            }

            if (ordery == null || !ordery.GetOrder().HasValue)
            {
                return -1;
            }

            return orderx.Order.CompareTo(ordery.Order);
        }
    }
}